#!/bin/bash
message "check uboot soucre ...."
if [ ! -d $OPI_U_source ]
then
	message "soucre not foud !"
	exit 0
fi

message "entry u-boot source dir " && cd $OPI_U_source

if [ ! -f .config ]
then 
	message "clear   u-boot " && make distclean
	message "cpoy $BOARD config file" && cp ${OPI_WORK_DIR}/config/uboot/$BOARD  ${OPI_U_source}/configs/${OPI_U_config}
	message "config $BOARD u-boot " && make $OPI_U_config
fi

#build uboot
echo ""
date
message "build u-boot , Please wait some time." && make -j8
if [ $? -eq 0 ]
then	
  printf "COMPILE OK.   %-50s \r\n" 
else
  printf "COMPILE ERROR.%-50s \r\n" 
  exit 1
fi
if [ -f u-boot-sunxi-with-spl.bin  ]; then
	# Copy u-boot-sunxi-with-spl.bin to output
	cp u-boot-sunxi-with-spl.bin  $OPI_UBOOT_OUTPUT_DIR 
	cp arch/arm/dts/*sun8i-h3*  $OPI_DTB_OUTPUT_DIR	
fi 
echo ""
date
message "build u-boot boot.scr" && rm -rf $OPI_UBOOT_OUTPUT_DIR/boot.scr  $OPI_UBOOT_OUTPUT_DIR/boot.cmd
mkimage -C none -A arm -T script -d $OPI_source/u-boot-script/orangepi.cmd $OPI_UBOOT_OUTPUT_DIR/boot.scr 
cp $OPI_source/u-boot-script/orangepi.cmd $OPI_UBOOT_OUTPUT_DIR/boot.cmd 
echo ""
date
message "build sunxi script.bin" && rm -rf $OPI_UBOOT_OUTPUT_DIR/script.bin $OPI_UBOOT_OUTPUT_DIR/script.fex
fex2bin $OPI_source/sunxi-script/orangepi.fex  $OPI_UBOOT_OUTPUT_DIR/script.bin
cp $OPI_source/sunxi-script/orangepi.fex  $OPI_UBOOT_OUTPUT_DIR/script.fex 
echo ""
date
message "build u-boot finish,Please to see $OPI_UBOOT_OUTPUT_DIR directories" 
echo "^_^"
